From 5d8f2177c547f0c73a895e569e09d12c086c0d04 Mon Sep 17 00:00:00 2001 From: Roan Kattouw Date: Mon, 10 Dec 2007 15:55:12 +0000 Subject: [PATCH] API: * Add list=allcategories module * Fix token-related E_NOTICEs --- RELEASE-NOTES | 2 +- includes/AutoLoader.php | 1 + includes/api/ApiQuery.php | 1 + includes/api/ApiQueryAllCategories.php | 141 +++++++++++++++++++++++++ includes/api/ApiQueryInfo.php | 3 + includes/api/ApiQueryRevisions.php | 1 + 6 files changed, 148 insertions(+), 1 deletion(-) create mode 100644 includes/api/ApiQueryAllCategories.php diff --git a/RELEASE-NOTES b/RELEASE-NOTES index 31f7dafaef..37b44c58af 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -346,7 +346,7 @@ Full API documentation is available at http://www.mediawiki.org/wiki/API * Fixed sessionid attribute in action=login * Standardized limits. Revisions and Deletedrevisions formerly using 200 / 10000, now 500 / 5000, in line with other modules. - +* Added list=allcategories module === Languages updated in 1.12 === diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index ec533ffe81..2a01444f7c 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -320,6 +320,7 @@ function __autoload($className) { 'ApiQuery' => 'includes/api/ApiQuery.php', 'ApiQueryAllpages' => 'includes/api/ApiQueryAllpages.php', 'ApiQueryAllLinks' => 'includes/api/ApiQueryAllLinks.php', + 'ApiQueryAllCategories' => 'includes/api/ApiQueryAllCategories.php', 'ApiQueryAllUsers' => 'includes/api/ApiQueryAllUsers.php', 'ApiQueryBase' => 'includes/api/ApiQueryBase.php', 'ApiQueryGeneratorBase' => 'includes/api/ApiQueryBase.php', diff --git a/includes/api/ApiQuery.php b/includes/api/ApiQuery.php index 40485adf1c..c42e7c98f1 100644 --- a/includes/api/ApiQuery.php +++ b/includes/api/ApiQuery.php @@ -60,6 +60,7 @@ class ApiQuery extends ApiBase { private $mQueryListModules = array ( 'allpages' => 'ApiQueryAllpages', 'alllinks' => 'ApiQueryAllLinks', + 'allcategories' => 'ApiQueryAllCategories', 'allusers' => 'ApiQueryAllUsers', 'backlinks' => 'ApiQueryBacklinks', 'blocks' => 'ApiQueryBlocks', diff --git a/includes/api/ApiQueryAllCategories.php b/includes/api/ApiQueryAllCategories.php new file mode 100644 index 0000000000..04f4300935 --- /dev/null +++ b/includes/api/ApiQueryAllCategories.php @@ -0,0 +1,141 @@ +.@home.nl + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * http://www.gnu.org/copyleft/gpl.html + */ + +if (!defined('MEDIAWIKI')) { + // Eclipse helper - will be ignored in production + require_once ('ApiQueryBase.php'); +} + +/** + * Query module to enumerate all categories, even the ones that don't have + * category pages. + * + * @addtogroup API + */ +class ApiQueryAllCategories extends ApiQueryGeneratorBase { + + public function __construct($query, $moduleName) { + parent :: __construct($query, $moduleName, 'ac'); + } + + public function execute() { + $this->run(); + } + + public function executeGenerator($resultPageSet) { + $this->run($resultPageSet); + } + + private function run($resultPageSet = null) { + + $db = $this->getDB(); + $params = $this->extractRequestParams(); + + $this->addTables('categorylinks'); + $this->addFields('cl_to'); + + if (!is_null($params['from'])) + $this->addWhere('cl_to>=' . $db->addQuotes(ApiQueryBase :: titleToKey($params['from']))); + if (isset ($params['prefix'])) + $this->addWhere("cl_to LIKE '" . $db->escapeLike(ApiQueryBase :: titleToKey($params['prefix'])) . "%'"); + + $this->addOption('LIMIT', $params['limit']+1); + $this->addOption('ORDER BY', 'cl_to' . ($params['dir'] == 'ZtoA' ? ' DESC' : '')); + $this->addOption('DISTINCT'); + + $res = $this->select(__METHOD__); + + $pages = array(); + $count = 0; + while ($row = $db->fetchObject($res)) { + if (++ $count > $params['limit']) { + // We've reached the one extra which shows that there are additional cats to be had. Stop here... + // TODO: Security issue - if the user has no right to view next title, it will still be shown + $this->setContinueEnumParameter('from', ApiQueryBase :: keyToTitle($row->cl_to)); + break; + } + + // Normalize titles + if(!is_null($resultPageSet)) + $titleObj = Title::newFromText('Category:' . $row->cl_to); + else + $titleObj = Title::newFromText($row->cl_to); + $pages[] = $titleObj->getPrefixedText(); + } + $db->freeResult($res); + + if (is_null($resultPageSet)) { + $result = $this->getResult(); + $result->setIndexedTagName($pages, 'c'); + $result->addValue('query', $this->getModuleName(), $pages); + } else { + $resultPageSet->populateFromTitles($pages); + } + } + + protected function getAllowedParams() { + return array ( + 'from' => null, + 'prefix' => null, + 'dir' => array( + ApiBase :: PARAM_DFLT => 'AtoZ', + ApiBase :: PARAM_TYPE => array( + 'AtoZ', + 'ZtoA' + ), + ), + 'limit' => array ( + ApiBase :: PARAM_DFLT => 10, + ApiBase :: PARAM_TYPE => 'limit', + ApiBase :: PARAM_MIN => 1, + ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1, + ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2 + ) + ); + } + + protected function getParamDescription() { + return array ( + 'from' => 'The category to start enumerating from.', + 'prefix' => 'Search for all category titles that begin with this value.', + 'dir' => 'Direction to sort in.', + 'limit' => 'How many categories to return.' + ); + } + + protected function getDescription() { + return 'Enumerate all categories'; + } + + protected function getExamples() { + return array ( + 'api.php?action=query&generator=allcategories&gacprefix=List&prop=info', + ); + } + + public function getVersion() { + return __CLASS__ . ': $Id: ApiQueryAllLinks.php 28216 2007-12-06 18:33:18Z vasilievvv $'; + } +} diff --git a/includes/api/ApiQueryInfo.php b/includes/api/ApiQueryInfo.php index 39a6c80ea4..8bb80ef0a5 100644 --- a/includes/api/ApiQueryInfo.php +++ b/includes/api/ApiQueryInfo.php @@ -66,6 +66,9 @@ class ApiQueryInfo extends ApiQueryBase { $tok_protect = $this->getTokenFlag($token, 'protect'); $tok_move = $this->getTokenFlag($token, 'move'); } + else + // Fix E_NOTICEs about unset variables + $token = $tok_edit = $tok_delete = $tok_protect = $tok_move = null; $pageSet = $this->getPageSet(); $titles = $pageSet->getGoodTitles(); diff --git a/includes/api/ApiQueryRevisions.php b/includes/api/ApiQueryRevisions.php index d5a108d4c8..4eb0e2a127 100644 --- a/includes/api/ApiQueryRevisions.php +++ b/includes/api/ApiQueryRevisions.php @@ -85,6 +85,7 @@ class ApiQueryRevisions extends ApiQueryBase { $this->fld_timestamp = $this->addFieldsIf('rev_timestamp', isset ($prop['timestamp'])); $this->fld_comment = $this->addFieldsIf('rev_comment', isset ($prop['comment'])); $this->fld_size = $this->addFieldsIf('rev_len', isset ($prop['size'])); + $this->tok_rollback = false; // Prevent PHP undefined property notice if(!is_null($token)) { $this->tok_rollback = $this->getTokenFlag($token, 'rollback'); -- 2.20.1